﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace game
{
    public class BillboardManager
    {
        protected Scene scene;

        protected AnimatedBillboard.Factory animBillboardFactory;
        protected Billboard.Factory billboardFactory;

        protected List<AnimatedBillboard> animBills = new List<AnimatedBillboard>();
        protected List<Billboard> gunShots = new List<Billboard>();

        protected Texture gunShotTex;
        protected Texture plasmaShotTex;

        protected Texture blasterShotBlueTex;
        protected Texture blasterShotRedTex;
        protected Texture blasterShotGreenTex;

        protected Texture fireExplTex;
        protected Texture thunderShotTex;

        public BillboardManager(Scene scene)
        {
            this.scene = scene;

            animBillboardFactory = new AnimatedBillboard.Factory(scene, 10, "Textures\\RedExplosion", 12, 120, 120, 60, false);
            billboardFactory = new Billboard.Factory(scene, 100);

            gunShotTex = ShooterGame.Instance.Content.Load<Texture>("Textures\\GunShot3");
            plasmaShotTex = ShooterGame.Instance.Content.Load<Texture>("Textures\\8195-v8");

            blasterShotBlueTex = ShooterGame.Instance.Content.Load<Texture>("Textures\\blasterShotBlue");
            blasterShotRedTex = ShooterGame.Instance.Content.Load<Texture>("Textures\\blasterShotRed");
            blasterShotGreenTex = ShooterGame.Instance.Content.Load<Texture>("Textures\\blasterShotGreen");

            fireExplTex = ShooterGame.Instance.Content.Load<Texture>("Textures\\SkybusterExplosion");
            thunderShotTex = ShooterGame.Instance.Content.Load<Texture>("Textures\\thunderbolt-hit");
        }

        public bool StartFireExplosion(Vector3 position, int size)
        {
            AnimatedBillboard ab = animBillboardFactory.Get();
            ab.Reset();
            ab.Position = position;
            ab.SetSize(2*size, 2*size); // maybe scale to fit ratio of texture
            animBills.Add(ab);

            return true;
        }

        public bool GunShot(Vector3 position)
        {
            Billboard gunShot = billboardFactory.Get();
            gunShot.Reset();
            gunShot.Position = position;
            gunShot.MaxLifeTimeMs = 30.0f;
            gunShot.SetTexture(gunShotTex);
            gunShot.SetSize(200, 200);
            gunShots.Add(gunShot);

            return true;
        }

        public bool PlasmaShot(Vector3 position, Vector3 velocity)
        {
            Billboard gunShot = billboardFactory.Get();
            gunShot.Reset();
            gunShot.Position = position;
            gunShot.MaxLifeTimeMs = 50.0f;
            gunShot.SetTexture(plasmaShotTex);
            gunShot.SetSize(600, 600);
            gunShot.Velocity = velocity;
            //gunShot.SetAxis(new Vector3(0,0,1));
            gunShots.Add(gunShot);

            return true;
        }

        public bool BlasterShot(Vector3 position, Vector3 velocity, BlasterCell.Color color)
        {
            Billboard gunShot = billboardFactory.Get();
            gunShot.Reset();
            gunShot.Position = position;
            gunShot.MaxLifeTimeMs = 50.0f;

            if(color == BlasterCell.Color.BLUE)
                gunShot.SetTexture(blasterShotBlueTex);
            else if(color == BlasterCell.Color.RED)
                gunShot.SetTexture(blasterShotRedTex);
            else
                gunShot.SetTexture(blasterShotGreenTex);

            gunShot.SetSize(600, 600);
            gunShot.Velocity = velocity;
            gunShot.Fading = true;
            gunShots.Add(gunShot);

            return true;
        }

        public bool ThunderShot(Vector3 position, Vector3 velocity, int scaleFactor)
        {
            Billboard gunShot = billboardFactory.Get();
            gunShot.Reset();
            gunShot.Position = position;
            gunShot.MaxLifeTimeMs = 400.0f;
            gunShot.SetTexture(thunderShotTex);
            gunShot.SetSize(1500*scaleFactor, 1500*scaleFactor);
            gunShot.Velocity = Vector3.Zero;
            gunShot.Fading = true;
            gunShots.Add(gunShot);

            return true;
        }

        public void Update(GameTime gameTime)
        {
            for (int i = 0; i < animBills.Count; i++)
            {
                AnimatedBillboard ab = animBills[i];
                ab.Update(gameTime);
                if (!ab.IsAlive)
                {
                    animBills.Remove(ab);
                    i--;
                    animBillboardFactory.Return(ab);
                }
            }

            for (int i = 0; i < gunShots.Count; i++)
            {
                Billboard b = gunShots[i];
                b.Update(gameTime);
                if (!b.IsAlive)
                {
                    gunShots.Remove(b);
                    i--;
                    billboardFactory.Return(b);
                }
            }
        }

        public void Draw(GameTime gameTime)
        {
            foreach (AnimatedBillboard a in animBills)
            {
                a.Draw(gameTime);
            }

            foreach (Billboard b in gunShots)
            {
                b.Draw(gameTime);
            }
        }
    }
}
